﻿namespace RuoVea.Swms.Web.Controllers;

/// <summary>
/// 入库管理
/// </summary>
[Authorize(Roles = "None")]
public class StockInController : Controller
{
    private readonly InventoryService _thisService;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="inventoryService"></param>
    public StockInController(InventoryService inventoryService)
    {
        _thisService = inventoryService;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public IActionResult index()
    {
        return View();
    }

    /// <summary>
    /// 入库记录分页查询
    /// </summary>
    [HttpPost("inventory/stockIn/pages")]
    public async Task<RestfulResult<PageResult<WmsStockIn>>> GetStockInPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception("未提供上传数据。请在Request.Body中携带上传数据。");
        StockInPageParam? data = JsonConvert.DeserializeObject<StockInPageParam>(dataStr);
        data ??= new StockInPageParam { PageNo = 1, PageSize = 10 };

        var query = await _thisService.GetStockInPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 创建入库单
    /// </summary>
    [UnitOfWork]
    [HttpPost("inventory/stockIn/add")]
    public async Task<RestfulResult> CreateStockInAsync([FromBody] WmsStockIn data)
    {
        var restful = await _thisService.CreateStockInAsync(data);
        return restful;
    }
}